//
//  EmoticonPackage.swift
//  Emoticon
//
//  Created by 李锡鹏 on 2017/6/10.
//  Copyright © 2017年 李锡鹏. All rights reserved.
//

import Foundation
class  EmoticonPackage: NSObject {
    // 表情包路径
    var id: String?
    // 表情表名字
    var group_naem_cn: String?
    // 表情包数组
    lazy var emoticons = [Emoticon]()
    
    init(dict:[String:AnyObject]) {
        super.init()
        id = dict["id"] as? String
        group_naem_cn = dict["group_name_cn"] as? String
        if let array = dict["emoticons"]  as? [[String:String]] {
            var index = 0
            for var d in array {
                if let png = d["png"],let dir=id {
                    d["png"] = dir + "/" + png
                }
                emoticons.append(Emoticon.init(dict: d as [String : AnyObject]))
                index += 1
                if index == 20 {
                    emoticons.append(Emoticon.init(isRemoved: true))
                    index = 0
                }
            }
            appendEmptyButton()
        }
        else
        {
            for _ in 0 ..< 20 {
                self.emoticons.append(Emoticon.init(isEmpty: true))
            }
            emoticons.append(Emoticon.init(isRemoved: true))
        }
    }
    func appendEmptyButton() {
        let count =  emoticons.count % 21
        if count > 0 && emoticons.count > 0 {
            for _ in count ..< 20 {
                emoticons.append(Emoticon.init(isEmpty: true))
            }
            emoticons.append(Emoticon.init(isRemoved: true))
        }
    }
    override var description: String {
        let keys = ["id","group_naem_cn","emoticons"]
        return dictionaryWithValues(forKeys: keys).description
    }
}
